﻿package org.edu.controller;





import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;


import net.sf.json.JSONObject;

import org.apache.struts2.ServletActionContext;
import org.edu.bean.User;
import org.edu.bean._navigationbar;
import org.edu.service.UserService;

import com.opensymphony.xwork2.ActionSupport;

public class LoginController extends ActionSupport {
	
	/**
	 *@author Impl
	 *@exception 登陆
	 */
	private static final long serialVersionUID = 1L;
	@Resource
	private UserService userService;
	private int[] nav ;
	private String systemNumber ;
	private String pass ;
	private String loginMessage ;
	private String result ;
	public String execute(){
		User user = null ;
		HttpServletRequest request = ServletActionContext.getRequest();
		HttpSession session = request.getSession() ;
		session.removeAttribute("user");
		session.removeAttribute("_a_b_bars");
		if(systemNumber != null && systemNumber != ""){
			if(pass != null && pass != ""){
				user = userService.login(systemNumber, pass);
				if(user != null){
					session.setAttribute("user", user);
					return SUCCESS ;
				}else{
					loginMessage = "密码不匹配";
				}
			}else{
				loginMessage = "密码为空" ;
			}
		}else{
			loginMessage = "编号为空" ;
		}
		return ERROR;
	}
	public String logout(){
		HttpServletRequest request = ServletActionContext.getRequest();
		HttpSession session = request.getSession() ;
		session.invalidate() ;
		Map map = new HashMap();  
        map.put("status", true);
		JSONObject object = JSONObject.fromObject(map);
		result = object.toString();
		return SUCCESS;
	}
	/**
	 * @exception 帮助
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public String help(){
		HttpServletRequest request = ServletActionContext.getRequest();
		HttpSession session = request.getSession() ;
		List<_navigationbar> list= (List<_navigationbar>)session.getAttribute("_Navigationbars");
		nav = new int[7];
		for(int i= 0 ; i<list.size() ; i++){
			int num = ((_navigationbar)list.get(i)).getId();
			nav[num] = 1 ;
			if(num == 6){
				nav[0] = 1 ;
			}
		}
		return SUCCESS ;
	}
	public String getLoginMessage() {
		return loginMessage;
	}

	public void setLoginMessage(String loginMessage) {
		this.loginMessage = loginMessage;
	}

	public String getSystemNumber() {
		return systemNumber;
	}
	public void setSystemNumber(String systemNumber) {
		this.systemNumber = systemNumber;
	}
	public String getPass() {
		return pass;
	}
	public void setPass(String pass) {
		this.pass = pass;
	}
	public String getResult() {
		return result;
	}
	public void setResult(String result) {
		this.result = result;
	}
	public int[] getNav() {
		return nav;
	}
	public void setNav(int[] nav) {
		this.nav = nav;
	}


}
